<?php

namespace common\widgets\grid;

use Closure;
use yii\grid\Column;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\data\ArrayDataProvider;
use yii\db\ActiveQueryInterface;
use yii\helpers\Html;
use yii\helpers\Inflector;

/**
 * DataColumn is the default column type for the [[GridView]] widget.
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class DataColumn extends \yii\grid\DataColumn {

    /**
     * @var boolean 是否使用绝对时间戳
     */
    public $absoluteTimestamp = false;

    /**
     * @var string hint to be displayed in the [[header|header cell]] and also to be used as the sorting
     * link label when sorting is enabled for this column.
     * If it is not set and the models provided by the GridViews data provider are instances
     * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
     * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
     */
    public $hint;

    /**
     * {@inheritdoc]
     * @since 2.0.8
     */
    protected function getHeaderCellLabel() {
        $provider = $this->grid->dataProvider;
        $hint = null;
        if ($this->label === null) {
            if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
                /* @var $modelClass Model */
                $modelClass = $provider->query->modelClass;
                $model = $modelClass::instance();
                $label = $model->getAttributeLabel($this->attribute);
                $hint = $model->getAttributeHint($this->attribute);
            } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
                /* @var $modelClass Model */
                $modelClass = $provider->modelClass;
                $model = $modelClass::instance();
                $label = $model->getAttributeLabel($this->attribute);
                $hint = $model->getAttributeHint($this->attribute);
            } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
                $label = $this->grid->filterModel->getAttributeLabel($this->attribute);
                $hint = $this->grid->filterModel->getAttributeHint($this->attribute);
            } else {
                $models = $provider->getModels();
                if (($model = reset($models)) instanceof Model) {
                    /* @var $model Model */
                    $label = $model->getAttributeLabel($this->attribute);
                    $hint = $model->getAttributeHint($this->attribute);
                } else {
                    $label = Inflector::camel2words($this->attribute);
                }
            }
        } else {
            $label = $this->label;
        }
        if ($this->hint || $hint) {
            $this->encodeLabel = false;
            $label .= Html::tag('span', $this->hint ?: $hint, ['class' => 'grid-label-hint']);
        }

        return $label;
    }

    /**
     * {@inheritdoc}
     */
    protected function renderDataCellContent($model, $key, $index) {
        if ($this->content === null) {
            $value = $this->getDataCellValue($model, $key, $index);
            if ($this->format === 'datetime' || $this->format === 'date') {
                if ($this->absoluteTimestamp === false && !$value) {
                    return $this->grid->emptyCell;
                }
            }
            return $this->grid->formatter->format($value, $this->format);
        }
        return parent::renderDataCellContent($model, $key, $index);
    }

}
